/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.marcelo.comun.clasesAbstractas;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Collection;
import javax.swing.*;
import org.marcelo.agro.constantes.IConstantes;
import org.marcelo.comun.habilidades.Delete;
import org.marcelo.comun.habilidades.Reload;
import org.marcelo.comun.notificaciones.Notificaciones;
import org.netbeans.api.actions.Editable;
import org.openide.explorer.view.OutlineView;
import org.openide.util.Utilities;

/**
 *
 * @author marcelo
 */
public class DefaultOutlineView extends OutlineView {

    public DefaultOutlineView() {
        init();
        
    }

    public DefaultOutlineView(String nodesColumnLabel) {
        super(nodesColumnLabel);
        init();
        
    }
    
    private void init() {
        
        getOutline().setToolTipText(null);
        int condicion = JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT;
        
        InputMap inputMap = getOutline().getInputMap(condicion);
        
        //Creando las entradas de datos
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0), IConstantes.EDIT);
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0), IConstantes.NEW);
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), IConstantes.DELETE);
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0), IConstantes.REFRESH);
        
        ActionMap actionMap = getOutline().getActionMap();
        
        actionMap.put(IConstantes.EDIT, new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Editable edit = Utilities.actionsGlobalContext().lookup(Editable.class);
                if (edit != null)
                    edit.edit();
            }
        });
        
        actionMap.put(IConstantes.REFRESH, new AbstractAction() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Reload reload = Utilities.actionsGlobalContext().lookup(Reload.class);
                if (reload != null)
                    reload.reload();
            }
        });
        
        actionMap.put(IConstantes.DELETE, new AbstractAction() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Collection<? extends Delete> deleteList = Utilities.actionsGlobalContext().lookupAll(Delete.class);
                if (deleteList != null) {
                    int contErrors= 0;
                    for (Delete d: deleteList) {
                        try {
                            d.delete();
                        }
                        catch(Exception e1) {
                            contErrors ++;
                        }
                    }
                    if (contErrors > 0){
                        Notificaciones.showMessageBox(Notificaciones.WARNING_MESSAGE, 
                                IConstantes.MENSAJE_CANTIDAD_NO_BORRADOS
                                );
                    }
                }
                    
            }
        });
    }
   
    
}
